package io.dcloud.h527baaa6.utils;

import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.text.TextUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import io.dcloud.h527baaa6.bean.EventMediaBean;


/**
 * Created by hunanqi on 2017/8/2 0002.\
 * 播放声音的util
 */

public class PlayUtil {
    private MediaPlayer mediaPlayer;
    private static volatile PlayUtil instance;
    private CountDownTimer timer;
    private Timer mtimer;
    private MyTimerTask task;

    public PlayUtil() {

    }

    public static PlayUtil getIns() {
        if (instance == null) {
            synchronized (PlayUtil.class) {
                if (instance == null) {
                    instance = new PlayUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 启动声音播放,
     * 防止用户点击过多延迟1秒再放
     *
     * @param videoUrl
     */
    public void startPlay(final String videoUrl) {
        if (timer != null) {
            timer.cancel();
        }
        timer = new CountDownTimer(1000, 100) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                play(videoUrl);
            }
        };
        timer.start();
    }

    /**
     * 正常播放
     * @param videoUrl
     */
    public void startNormalPlay(String videoUrl){
        play(videoUrl);
    }
    /**
     * 播放
     *
     * @param videoUrl
     */
    private void play(String videoUrl) {
        cancleTimer();
        if (TextUtils.isEmpty(videoUrl))
            return;
        if (mediaPlayer == null)
            mediaPlayer = new MediaPlayer();
        if (mediaPlayer.isPlaying())
            mediaPlayer.stop();
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(videoUrl);
            mediaPlayer.setOnPreparedListener(mOnPreparedListener);
            mediaPlayer.setOnCompletionListener(mOnCompletionListener);
            mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止声音播放
     */
    public void stopPlay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying())
            mediaPlayer.stop();
            cancleTimer();
    }

    /**
     * 暂停
     */
    public void onPause(){
        if (mediaPlayer != null && mediaPlayer.isPlaying())
            mediaPlayer.pause();
        cancleTimer();
    }

    /**
     * 播放
     */
    public void onResum(){
        if (mediaPlayer!=null)
            mediaPlayer.start();
        startTimer();
    }
    /**
     * 音频监听方法
     */
    MediaPlayer.OnPreparedListener mOnPreparedListener = new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
            startTimer();
        }
    };
    MediaPlayer.OnCompletionListener mOnCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            cancleTimer();
            EventBus.getDefault().post(new EventMediaBean(0,0,true));
        }
    };

    private void startTimer() {
        try {
            mtimer = new Timer();
            task = new MyTimerTask();
            mtimer.schedule(task, 0, 1000);//开始定时器
        } catch (Exception e) {

        }

    }

    private void cancleTimer() {
        try {
            if (mtimer != null)
                mtimer.cancel();
        } catch (Exception e) {

        }
    }

    //定时器
    class MyTimerTask extends TimerTask {

        @Override
        public void run() {
            if (mediaPlayer != null) {
                 sendProgress();
            }
        }
    }
    //得到时间
    private void sendProgress(){
        if (mediaPlayer!=null){
            int currentPosition = mediaPlayer.getCurrentPosition();
            int duration = mediaPlayer.getDuration();
            EventBus.getDefault().post(new EventMediaBean(currentPosition,duration,false));
        }
    }
}
